package com.cn.designpatterns.ex.concrete.iterator;

import java.util.*;

/**
 * only implement list collection iterator,other not support
 *
 * @author zhangyi
 * @date 2018/12/25 17:41
 */
public class IteratorImpl implements Container {

    private List<Integer> list;
    private Set<Integer> set;
    public IteratorImpl(Object li) {
        if(li instanceof List){
            List list = (List)li;
            this.list = list;
        }else if(li instanceof Set){
            Set set = (Set)li;
            this.set = set;
        }
    }

    @Override
    public MyIterator getIterator() {
        return new InnerIterator();
    }

    private class InnerIterator implements MyIterator{
        int index = 0;
        @Override
        public boolean hasNext() {
            int size = 0;
            if(Objects.isNull(list)){
                size = set.size();
            }else {
                size = list.size();
            }
            return index >= size ? false : true;
        }

        @Override
        public Object next() {
            if(Objects.isNull(list)) {
                set.stream().forEach(s -> System.out.println(s));
                index = Integer.MAX_VALUE;
                return "set 遍历结束";
            }else{
                return list.get(index++);
            }
        }
    }
}
